package com.huika.dealontop.control.base.extend;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.huika.dealontop.R;
import com.huika.dealontop.utils.UIUtils;

/**
 * Author: Victor Wu
 * Email: victor-wu*foxmail.com
 * Time: 2015/11/12 12:11
 * Description: 标题栏帮助类
 */
public class ActionBarHelper {
    private ViewGroup mRootView;
    private View titleBar;
    private TextView left;
    private TextView right;
    private TextView title;
    private ImageView rightImg;

    public ActionBarHelper(ViewGroup rootView) {
        this.mRootView = rootView;
        this.titleBar = mRootView.findViewById(R.id.rl_title);
        this.left = (TextView) mRootView.findViewById(R.id.left);
        Drawable leftDrawable = UIUtils.getResources().getDrawable(R.mipmap.ssdk_back_arr);
        leftDrawable.setBounds(0, 0, UIUtils.dip2px(19), UIUtils.dip2px(20));
        left.setCompoundDrawables(leftDrawable, null, null, null);
        this.title = (TextView) mRootView.findViewById(R.id.title);
        this.right = (TextView) mRootView.findViewById(R.id.right);
        this.rightImg = (ImageView) mRootView.findViewById(R.id.title_right_iv);
    }

//	/**
//	 * 显示左侧返回图标、标题和右侧标题
//	 */
//	public ActionBarHelper(Activity activity, int rightStrId, int titleStrId) {
//		this(activity, -1, rightStrId, titleStrId);
//		left.setVisibility(View.VISIBLE);
//		left.setText("");
//	}

//	/**
//	 * 左侧优先显示上一个activity的title
//	 */
//	public ActionBarHelper(Activity activity, int leftStrId, int rightStrId, int titleStrId) {
//		mActivity = activity;
//		if (titleStrId != -1) {
//			mActivity.setTitle(titleStrId);
//		}
//		left = (TextView) activity.findViewById(R.id.left);
//		left.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				mActivity.onBackPressed();
//			}
//		});
//
//		setLeftMsg(leftStrId);
//		if (mActivity.getIntent() != null) {// 优先显示intent传进来的左侧标题
//			String backStr = mActivity.getIntent().getStringExtra(BaseAct.LEFT_TITLE);
//			if (!TextUtils.isEmpty(backStr)) {
//				setLeftMsg(backStr);
//			}
//		}
//		right = (TextView) activity.findViewById(R.id.right);
//		setRightMsg(rightStrId);
//		rightImg = (ImageView) activity.findViewById(R.id.title_right_iv);
//		title = (TextView) activity.findViewById(R.id.title);
//		setTitleMsg(titleStrId);
//	}

    /**
     * 右侧 begin
     *********************/
    public void setRightMsg(int rightStrId) {
        if (rightStrId == -1) {
            right.setVisibility(View.GONE);
        } else {
            right.setVisibility(View.VISIBLE);
            right.setText(rightStrId);
        }
    }

    public void setRightMsg(String msg) {
        if(TextUtils.isEmpty(msg)){
            right.setVisibility(View.GONE);
            right.setText("");
        }else{
            right.setVisibility(View.VISIBLE);
            right.setText(msg);
        }
    }

    public void setOnRightClickListener(View.OnClickListener listener) {
        right.setOnClickListener(listener);
    }

    // 添加右边图标显示；
    public void setRightImg(int rightSrc) {
        rightImg.setVisibility(View.VISIBLE);
        rightImg.setImageResource(rightSrc);
    }

    public void setOnRightImgClickListener(View.OnClickListener listener) {
        rightImg.setOnClickListener(listener);
    }

    public void hideActionBar() {
        titleBar.setVisibility(View.GONE);
    }

    public void hideRightImg() {
        rightImg.setVisibility(View.GONE);
    }

    /** 右侧 end *********************/

    /**
     * 左侧 begin
     *********************/
    public void setLeftMsg(int leftStrId) {
        if (leftStrId == -1) {
            left.setVisibility(View.GONE);
        } else {
            left.setVisibility(View.VISIBLE);
            left.setText(leftStrId);
        }
    }

    public void setLeftMsg(String msg) {
        left.setVisibility(View.VISIBLE);
        left.setText(msg);
    }

    public void setOnLeftClickListener(View.OnClickListener listener) {
        left.setVisibility(View.VISIBLE);
        left.setOnClickListener(listener);
    }

    /** 左侧 end *********************/

    /**
     * 标题 begin
     *********************/
    public void setTitleMsg(int titleStrId) {
        if (titleStrId == -1) {
            title.setVisibility(View.GONE);
        } else {
            title.setVisibility(View.VISIBLE);
            title.setText(titleStrId);
        }
    }

    public void setTitleMsg(String msg) {
        title.setVisibility(View.VISIBLE);
        title.setText(msg);
    }
    /** 标题 end *********************/
}
